// 该文件用于创建整个应用的路由器
import VueRouter from "vue-router"

import About from "../pages/About.vue"
import Home from "../pages/Home.vue"
import News from "../pages/News.vue"
import Message from "../pages/Message.vue"
import Detail from "../pages/Detail.vue"

// 创建一个路由器
const router = new VueRouter({
    mode: "history",
    routes: [
        {
            // name跳转时减少编码
            name: "guanyu",
            path: "/about",
            component: About,
            meta: { isAuth: true, title: "关于" }
        },
        {
            name: "zhuye ",
            path: "/home",
            component: Home,
            meta: { title: "首页" },
            children: [
                {
                    name: "xinwen",
                    path: "news",
                    component: News,
                    meta: { isAuth: true, title: "新闻" },
                    // beforeEnter: (to, from, next) => {
                    //     if (to.meta.isAuth) {//判定是否需要权限
                    //         if (localStorage.getItem("name") === "song") {
                    //             next()
                    //         } else {
                    //             alert("用户名不对无权查看")
                    //         }
                    //     } else {
                    //         next()
                    //     }
                    // }
                },
                {
                    name: "xiaoxi",
                    path: "message",
                    component: Message,
                    meta: { isAuth: true, title: "消息" },
                    children: [
                        {
                            name: "xiangqing",
                            path: "detail/:id/:title",
                            component: Detail,
                            meta: { title: "详情" },
                            // props写法一，该对象中所有key-value都会以props形式传
                            // props: { a: 1, b: "hello" }

                            // props写法二一，值为布尔值，若布尔值为真，将收到的所有params参数以props参数传
                            //  props: true

                            // props写法三，值为函数
                            props($route) {
                                return { id: $route.query.id, title: $route.query.title }
                            }

                            // props({ query: { id, title } }) {
                            //     return { id, title }
                            // }
                        }
                    ]
                }
            ]
        }
    ]
})
// 全局前置路由守卫——每次路由切换之前和初始化时被调用
// //（比如有特定的需求跳转路由
// router.beforeEach((to, from, next) => {
//     console.log("前置守卫路由", to, from)
//     // if (to.path === "/home/news" || to.path === "/home/message")
//     // if (to.name === "xinwen" || to.name === "xiaoxi")
//     if (to.meta.isAuth) {//判定是否需要权限
//         if (localStorage.getItem("name") === "song") {
//             // document.title = to.meta.title || 'sec_test'
//             next()
//         } else {
//             alert("用户名不对无权查看")
//         }
//     } else {
//         // document.title = to.meta.title || 'sec_test'
//         next()
//     }
// })

// 全局后置路由守卫——每次路由切换之后和初始化时被调用
//没有next
//作用：
// router.afterEach((to, from) => {
//     document.title = to.meta.title || 'sec_test'
//     console.log("后置守卫路由", to, from)
// })

export default router